home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13980 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How to use assert( )
  5. Date: 10 Apr 1996 14:10:11 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4kh83jINN2js@keats.ugrad.cs.ubc.ca>
  8. References: <4kc3k7$dur@orion.cybercom.net> <DpnqFE.E8v@eskimo.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <DpnqFE.E8v@eskimo.com>, Steve Summit <scs@eskimo.com> wrote:
  12. >In article <4kc3k7$dur@orion.cybercom.net>, nield@cybercom.net writes:
  13. >> I'm just starting my first project big enough to split among many
  14. >> people, and from the vague explanations I've heard, assert is supposed
  15. >> to be a usefull way to cause errors when someone passes your code bad
  16. >> values.
  17. >
  18. >Indeed (as long as we understand that "someone" is one of your
  19. >fellow programmers, and *not* one of the program's users).
  20. >
  21. >An assertion (which is what C's assert() macro implements)
  22. >allows you to document the assumptions made by a piece of code,
  23. >and furthermore to arrange that the code test its assumptions
  24. >as it runs.  Usually, these assumptions boil down to "the rest
  25. >of the program is written correctly," and in a large program,
  26. >particularly one worked on by many people, this is of course
  27. >*not* always a valid assumption, and so is eminently worth
  28. >testing.
  29.  
  30. The assumptions boil down to WP's---weakest preconditions.  These are the
  31. sufficient initial conditions for the correct computation of a result by a
  32. given statement or statements.
  33.  
  34. For example, the weakest precondition for having x == 5 after the statement
  35. x += 2; is x == 3. Before executing the statement, we can assert (x == 3).
  36.  
  37. This is trivial, of course, but you can devise an arbitrarily complex system
  38. of correctness proof around this.
  39.  
  40. I suppose that one could also use assert() in conjuction with loop invariants.
  41. If I think I have identifed the loop invariant of some iterative algorithm, why
  42. not assert() the sucker in the implementation?  If it fails, either I have
  43. incorrectly identified the loop invariant or there is a bug in the
  44. implementation of the algorithm.
  45.  
  46. (Kaz is going to go assert() crazy now...)
  47. -- 
  48.  
  49.